﻿namespace td.Core
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;

    public class SplashScreenForm : Form
    {
        private Image bitmap;
        private static List<string> parameterList = new List<string>();
        private static List<string> requestedFileList = new List<string>();
        private static SplashScreenForm splashScreen;
        public const string VersionText = "td.AddinFramework ";

        public SplashScreenForm()
        {
            base.FormBorderStyle = FormBorderStyle.None;
            base.StartPosition = FormStartPosition.CenterScreen;
            base.ShowInTaskbar = false;
            string path = Application.StartupPath + @"\Images\SplashScreen.gif";
            string str2 = Application.StartupPath + @"\Images\SplashScreen.jpg";
            if (File.Exists(path))
            {
                this.bitmap = Image.FromFile(path);
            }
            if (File.Exists(str2))
            {
                this.bitmap = Image.FromFile(str2);
            }
            base.ClientSize = this.bitmap.Size;
            this.BackgroundImage = this.bitmap;
            base.MouseClick += new MouseEventHandler(this.SplashScreenForm_MouseClick);
            base.TopMost = false;

        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.bitmap != null))
            {
                this.bitmap.Dispose();
                this.bitmap = null;
            }
            base.Dispose(disposing);
        }

        public static string[] GetParameterList()
        {
            return parameterList.ToArray();
        }

        public static string[] GetRequestedFileList()
        {
            return requestedFileList.ToArray();
        }

        public static void SetCommandLineArgs(string[] args)
        {
            requestedFileList.Clear();
            parameterList.Clear();
            foreach (string str in args)
            {
                if (str.Length != 0)
                {
                    if ((str[0] == '-') || (str[0] == '/'))
                    {
                        int startIndex = 1;
                        if (((str.Length >= 2) && (str[0] == '-')) && (str[1] == '-'))
                        {
                            startIndex = 2;
                        }
                        parameterList.Add(str.Substring(startIndex));
                    }
                    else
                    {
                        requestedFileList.Add(str);
                    }
                }
            }
        }

        public static void ShowSplashScreen()
        {
            ShowSplashScreen("");
        }

        public static void ShowSplashScreen(string appInfo)
        {
            try
            {
                if (splashScreen == null)
                {
                    splashScreen = new SplashScreenForm();
                }
                string s = "";
                if ((appInfo != null) && !appInfo.Trim().Equals(""))
                {
                    s = appInfo + " (debug)";
                    using (Font font = new Font("隶书", 14f,FontStyle.Bold))//("Fixedsys", 12f))
                    {
                        
                        using (Graphics graphics = Graphics.FromImage(splashScreen.bitmap))
                        {
                            graphics.DrawString(s, font, Brushes.Blue, (float) 0f, (float) (splashScreen.bitmap.Height-font.Height*2));
                        }
                    }
                }
                if (splashScreen.bitmap != null)
                {                    
                    splashScreen.Show();

                }
            }
            catch(Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
        }
        /// <summary>
        /// 用于向SplashScreen输出信息
        /// </summary>
        /// <param name="Info"></param>
        public static void DrawInfo(string Info)
        {
            using (Font font = new Font("隶书", 14f, FontStyle.Bold))//("Fixedsys", 12f))
            {

                using (Graphics graphics = Graphics.FromImage(splashScreen.bitmap))
                {
                    graphics.DrawString(info+=Info, font, Brushes.Blue, (float)x, font.Height+5);
                    splashScreen.BackgroundImage = splashScreen.bitmap;
                    splashScreen.Invalidate();
                }
            }
        }
        private static string info = "";
        private static float x = 0;
        private static float y = 0;
        private void SplashScreenForm_MouseClick(object sender, MouseEventArgs e)
        {
            base.Visible = false;
        }

        public static SplashScreenForm SplashScreen
        {
            get
            {
                return splashScreen;
            }
            set
            {
                splashScreen = value;
            }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // SplashScreenForm
            // 
            this.ClientSize = new System.Drawing.Size(487, 339);
            this.Name = "SplashScreenForm";
            this.ResumeLayout(false);

        }
    }
}

